package test.adddata;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import utils.SmallTool;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author: gcr
 * @Date: 2022-5-17 14:42
 */
public class 合并数据网页年数据合并到刀4主表20220527 {
    @SneakyThrows
    public static void main(String[] args) {

        //2022年5月18日09:56:35
        // 用于合并网页时间数据


        String dbNameMy = "pubmed";
        String from = "gcr_pm_pub_year_copy_copy";

        int breakSize = 100000;

        // 下次接着跑
        int startIndex = 343;

        SmallTool.printMemory();
        Db local = Db.use();
        Db useServer = Db.use(DSFactory.get("server"));
        String allDate = "select max(pmid) as ids  from " + from;
        Entity query = local.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("ids"));

        Integer count = query.getInt("ids");
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSize; i++) {
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            if (i < startIndex) {
                continue;
            }
            SmallTool.printMess("开始页码++" + i + "++id++" + start + "结束id++++" + end);
            String meSql = "select id,pmid,html_year from " + dbNameMy + " where pmid between " + start + " and " + end;
            List<Entity> queryMe = useServer.query(meSql);
            queryMe.parallelStream().forEach(pmDeatil -> {
                Integer id = pmDeatil.getInt("id");
                Integer pmid = pmDeatil.getInt("pmid");
                String HTML_year = pmDeatil.getStr("html_year");
                if (HTML_year == null) {


                    Entity where = new Entity();
                    where.setTableName(dbNameMy);
                    where.set("id", id);
                    Entity exit = getExit(local, from, pmid);
                    if (exit != null) {
                        String year = exit.getStr("year").trim();
                        if (StringUtils.isNotEmpty(year) && year.length() == 4) {
                            pmDeatil.set("html_year", year);
                            try {
                                useServer.update(pmDeatil, where);
                            } catch (SQLException throwables) {
                                throwables.printStackTrace();
                            }
                        }

                    }
                }

            });

        }


    }

    @SneakyThrows
    public static Entity getExit(Db use, String table, Integer pmid) {
        Entity entity = use.queryOne("select * from " + table + " where pmid = ?", pmid);
        return entity;

    }


}

